package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
;
import java.util.List;
import java.util.Map;

/**
 * 客户管理层
 * @author 里
 * @Description:
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    //注入service 层
    @Autowired
    private CustomerService customerService;

    /**
     *  客户列表分页（名称模糊查询）
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public Map<String,Object> getCustomerListPage(@PathVariable Integer page,
                                                  @PathVariable Integer rows,
                                                  @PathVariable String  customerName) {
       return customerService.getCustomerListPage(page,rows,customerName);
    }


    @PostMapping("/save")
    @ResponseBody
    public ServiceVO saveOrUpdate(@RequestBody Customer customer) {
       //调用service层
        customerService.saveOrUpdateById(customer);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 根据id删除客户，支持批量删除
     * @param ids
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    @Transactional
    public ServiceVO deleteCustomer(List<String> ids) {
        //调用service层
        customerService.deleteCustomer(ids);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }



}
